<?php

namespace App\Http\Controllers\Admin;
// +----------------------------------------------------------------------
// | Copyright (c) 2024~至今 迪钰国际旅行社 版权所有.
// +----------------------------------------------------------------------
// | Author: dxf &lt;xiangfei2007@126.com&gt;
// +----------------------------------------------------------------------
// | Date: 2024/8/21 14:06
// +----------------------------------------------------------------------

use App\Exceptions\InternalException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\DictRequest;
use App\Http\Resources\DictResource;
use App\Logics\DictLogic;

class DictController extends Controller
{

    /**
     * 获取数据字典列表
     * @param DictRequest $request
     * @return mixed
     * Date:    2024/8/21 15:14
     */
    public function index(DictRequest $request): mixed
    {
        $params = $request->all();
        $page_size = $request->input('page_size', config('custom.page_size'));
        $lists = (new DictLogic())->index($params, $page_size);
        return $this->success([
            'data'  => DictResource::collection($lists),
            'total' => $lists->total()
        ]);
    }

    /**
     * 添加数据字典
     * @param DictRequest $request
     * @return mixed
     * Date:    2024/8/21 15:43
     */
    public function add(DictRequest $request): mixed
    {
        $params = $request->validated();
        $result = (new DictLogic())->add($params);
        return $this->message('添加成功');
    }

    /**
     * 修改数据字典
     * @param DictRequest $request
     * @param int $id
     * @return mixed
     * @throws InternalException
     * Date:    2024/8/23 10:59
     */
    public function edit(DictRequest $request, int $id): mixed
    {
        $params = $request->validated();

        $result = (new DictLogic())->edit($params, $id);
        return $this->message('修改成功');
    }

    /**
     * 删除数据字典
     * @param $id
     * @return mixed
     * Date:    2024/8/22 17:44
     */
    public function del($id): mixed
    {
        $result = (new DictLogic())->del($id);
        return $this->message('删除成功');
    }
}
